/*

Please compile using  gcc -g Example.c -lpthread -std=c99 -O
 */
 
#include <time.h>
#include <math.h>
#include <stdlib.h>
#include <time.h> 
#include <stdio.h>
#include <pthread.h>
#define SIZE    5

int  *array[SIZE];
void max(int  *);
void min(int  *);
void mean(int  *);

void randomfill(int  *);

 
int main(int argc,char *argv[])
{
randomfill(&array);
printf("\nProgram Running in Serial Code without Threads..\n");
//printf("\nIn SERIAL Code Randomly Generated array[SIZE](500 integers) in between 0 and 1000 is \n");
//for(int n=0; n<SIZE; n++)
//printf( "%d   ",array[n]);
//printf("\nNow, Running program's serail code:\n");
max(&array);
mean(&array);
min(&array);
printf("\n\nNow Running program's parallel code:\n");
//printf("\nIn PARALLEL Code Randomly Generated array[SIZE](500 integers) in between 0 and 1000 is \n");
int rt1,rt2,rt3;
pthread_t thread1,thread2,thread3;
 
  rt1=pthread_create(&thread2,
         NULL, max,
         (void *) array);
 
  rt2=pthread_create(&thread3,
         NULL, mean,
         (void *) array);
  rt3=pthread_create(&thread1,
         NULL,
         min,
         (void *) array);
  pthread_join(thread1, NULL);
  pthread_join(thread2, NULL);
  pthread_join(thread3, NULL);
return 0;
}
 
void max(int  *array1)
{
int maxi=array1[0];
for (int i=0;i<SIZE;i++)
if(maxi<array1[i])
maxi=array1[i];
printf("\nThe maximum of the given array is %d :",maxi);
}
 
void min(int  *array2)
{
int mi=array2[0];
for (int j=0;j<SIZE;j++)
if(mi>array2[j])
mi=array2[j];
printf("\nThe minimum  of the given array is %d :",mi);
}

void mean(int  *array3)
{
float mea=0.0,mea1=0.0;
for (int k=0;k<SIZE;k++)
mea=mea+array3[k];
mea1=mea/5;
printf("\nThe mean  of the given array is %.3f ",mea1);
}
void randomfill(int  *array)
{
 srand((unsigned)time(0));
 for(int m=0; m<SIZE; m++)
 array[m] = (rand()%1000)+0;
}
